package com.bootdo.wx.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.bootdo.common.utils.R;
import com.bootdo.common.utils.UUIDUtils;
import com.bootdo.common.utils.UuidUtil;
import com.bootdo.wx.dao.CmsContentDao;
import com.bootdo.wx.dao.CmsSignDao;
import com.bootdo.wx.dao.MemberScoreDao;
import com.bootdo.wx.dao.MemberSignDao;
import com.bootdo.wx.dao.MemberSignRewardDao;
import com.bootdo.wx.domain.CmsSignDO;
import com.bootdo.wx.domain.MemberSignDO;
import com.bootdo.wx.domain.MemberSignRewardDO;
import com.bootdo.wx.service.MemberSignService;


@Service
public class MemberSignServiceImpl implements MemberSignService {
	@Autowired
	private MemberSignDao memberSignDao;
	@Autowired
	private CmsContentDao cmsContentDao;
	@Autowired
	private MemberSignRewardDao memberSignRewardDao;
	@Autowired
	private CmsSignDao cmsSignDao;
	
	@Override
	public MemberSignDO get(String id){
		return memberSignDao.get(id);
	}
	
	@Override
	public List<MemberSignDO> list(Map<String, Object> map){
		return memberSignDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return memberSignDao.count(map);
	}
	
	@Override
	public int save(MemberSignDO memberSign){
		return memberSignDao.save(memberSign);
	}
	
	@Override
	public int update(MemberSignDO memberSign){
		return memberSignDao.update(memberSign);
	}
	
	@Override
	public int remove(String id){
		return memberSignDao.remove(id);
	}
	
	@Override
	public int batchRemove(String[] ids){
		return memberSignDao.batchRemove(ids);
	}

	@Override
	public List<MemberSignDO> getTodaySign(String userId) {
		return memberSignDao.getTodaySign(userId);
	}

	@Override
	public List<MemberSignDO> getSignList(Map<String, Object> map) {
		return memberSignDao.getSignList(map);
	}

	@Override
	@Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED) // 有事务则加入,没有则建立
	public R addSign(Map<String, Object> params) {
		Date date = new Date();
		SimpleDateFormat sdf1 =new SimpleDateFormat("yyyy-MM-dd" );
		String str1 = sdf1.format(date);
		String userId = params.get("userId").toString();
		MemberSignDO signDO = new MemberSignDO();
		signDO.setId(UuidUtil.get32UUID());
		signDO.setUserId(userId);
		signDO.setCreateAt(date);
		signDO.setDelFlag(0);
		signDO.setDate(str1);
		memberSignDao.save(signDO);
		CmsSignDO cmsSignDO = cmsSignDao.findByCurrentRule(str1);
		params.put("days", str1);
		params.put("delaFlag", "0");
		List<MemberSignRewardDO> list = memberSignRewardDao.list(params);
		if(cmsSignDO!=null&&list.size()<=0){
			Map<String, Object> map = new HashMap<>();
			map.put("startAt", cmsSignDO.getStartAt());
			map.put("endDate", cmsSignDO.getEndDate());
			map.put("userId", userId);
			int count = memberSignDao.memberSignCount(map);
			if(count==cmsSignDO.getDays()){
				MemberSignRewardDO memberSignRewardDO = new MemberSignRewardDO();
				memberSignRewardDO.setCaAt(new Date());
				memberSignRewardDO.setDays(str1);
				memberSignRewardDO.setDelFlag(0);
				memberSignRewardDO.setId(UUIDUtils.getUUID());
				memberSignRewardDO.setStatus(0);
				memberSignRewardDO.setUserId(userId);
				memberSignRewardDao.save(memberSignRewardDO);
				return R.ok("1").put("data",signDO);
			}
		}
		//插入积分
		/*BigDecimal value = new BigDecimal(cmsContentDao.getByType("score-sign").getValue());
		MemberScoreDO scoreDO = new MemberScoreDO();
		scoreDO.setId(UuidUtil.get32UUID());
		scoreDO.setUserId(userId);
		scoreDO.setMoney(value);
		scoreDO.setDelFlag(0);
		scoreDO.setCreateAt(date);
		scoreDO.setUpdateAt(date);
		scoreDO.setType(1);
		scoreDO.setStatus(1);
		scoreDO.setGId(signDO.getId());
		memberScoreDao.save(scoreDO);*/
		return R.ok().put("data",signDO);
	}
	
}
